package com.bkgy.jobs;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.controller.optimize.dao.OptimizeDao;
import com.bkgy.controller.optimize.dto.Body;
import com.bkgy.controller.optimize.dto.HmiBaseRequest;
import com.bkgy.controller.optimize.dto.HmiBaseResponse;
import com.bkgy.controller.optimize.entity.CaseManageEntity;
import com.bkgy.modular.httppost.HttpClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:Lipeng Zhao
 * Description:
 *
 * @Date:Created In 19:12 2025/8/11
 */
@Slf4j
@Service
public class RollingSimulationJobs {

    @Autowired
    private OptimizeDao optimizeDao;

    @Autowired
    private HttpClientService httpClientService;

    @Value("${hmi}")
    private String hmi;

    public void run() {
        try {
            List<CaseManageEntity> list = optimizeDao.findCaseMapByStatus();
            for(int i=0;i<list.size();i++){
                CaseManageEntity caseManage=new CaseManageEntity();
                caseManage.setCoilNo(list.get(i).getCoilNo());
                caseManage.setSteelGrade(list.get(i).getSteelGrade());
                caseManage.setYp(list.get(i).getYp());
                caseManage.setEnThick(list.get(i).getEnThick());
                caseManage.setExThick(list.get(i).getExThick());
                caseManage.setExWidth(list.get(i).getExWidth());
                caseManage.setL(list.get(i).getL());
                caseManage.setM(list.get(i).getM());
                caseManage.setN(list.get(i).getN());
                caseManage.setStatus(2);
                rollingSimulationCal(caseManage);
                Thread.sleep(5000);
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }

    }

    public void rollingSimulationCal(CaseManageEntity caseManage){


        Map map= new HashMap<String,String>();
        List<Map<String,String>> mapList=new ArrayList<>();
        HmiBaseRequest hmiBaseRequest=new HmiBaseRequest();
        map.put("CoilNo",caseManage.getCoilNo());
        map.put("SteelGrade",caseManage.getSteelGrade());
        map.put("YP",caseManage.getYp());
        map.put("res_EN_THICK",caseManage.getEnThick());
        map.put("res_EX_THICK",caseManage.getExThick());
        map.put("res_EX_WIDTH",caseManage.getExWidth());
        mapList.add(map);
        Body body=new Body();
        List<List<Map<String,String>>> list1=new ArrayList<>();
        list1.add(mapList);
        body.setParams(list1);
        hmiBaseRequest.setBody(body);
        String json = JSON.toJSONString(hmiBaseRequest);

//        HmiBaseResponse hmiBaseResponse=new HmiBaseResponse();
//        List list=new ArrayList<>();
//        hmiBaseResponse.setResults(new ArrayList<>());
        String respStr = httpClientService.postJsonRequest(hmi, json, false, "");
        HmiBaseResponse responseDto= JSONObject.parseObject(respStr,HmiBaseResponse.class);
        if(responseDto.getCode()==200){
            if(responseDto.getResults().size()==0){
                return;
            }
            Object object=responseDto.getResults().get(0);
            JSONArray picArray=JSONArray.parseArray(object.toString());
            JSONObject obj= picArray.getJSONObject(0);
            caseManage.setDeformRate1(BigDecimal.valueOf(Double.parseDouble(obj.get("Deform_Rate1").toString())));
            caseManage.setDeformRate2(BigDecimal.valueOf(Double.parseDouble(obj.get("Deform_Rate2").toString())));
            caseManage.setDeformRate3(BigDecimal.valueOf(Double.parseDouble(obj.get("Deform_Rate3").toString())));
            caseManage.setDeformRate4(BigDecimal.valueOf(Double.parseDouble(obj.get("Deform_Rate4").toString())));
            caseManage.setDeformRate5(BigDecimal.valueOf(Double.parseDouble(obj.get("Deform_Rate5").toString())));
            caseManage.setWrDia1(BigDecimal.valueOf(Double.parseDouble(obj.get("Diameter1").toString())));
            caseManage.setWrDia2(BigDecimal.valueOf(Double.parseDouble(obj.get("Diameter2").toString())));
            caseManage.setWrDia3(BigDecimal.valueOf(Double.parseDouble(obj.get("Diameter3").toString())));
            caseManage.setWrDia4(BigDecimal.valueOf(Double.parseDouble(obj.get("Diameter4").toString())));
            caseManage.setWrDia5(BigDecimal.valueOf(Double.parseDouble(obj.get("Diameter5").toString())));

            caseManage.setNr1(BigDecimal.valueOf(Double.parseDouble(obj.get("Roll_Num1").toString())));
            caseManage.setNr2(BigDecimal.valueOf(Double.parseDouble(obj.get("Roll_Num2").toString())));
            caseManage.setNr3(BigDecimal.valueOf(Double.parseDouble(obj.get("Roll_Num3").toString())));
            caseManage.setNr4(BigDecimal.valueOf(Double.parseDouble(obj.get("Roll_Num4").toString())));
            caseManage.setNr5(BigDecimal.valueOf(Double.parseDouble(obj.get("Roll_Num5").toString())));

            caseManage.setUnitTension1(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension1").toString())));
            caseManage.setUnitTension2(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension2").toString())));
            caseManage.setUnitTension3(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension3").toString())));
            caseManage.setUnitTension4(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension4").toString())));
            caseManage.setUnitTension5(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension5").toString())));
            caseManage.setUnitTension6(BigDecimal.valueOf(Double.parseDouble(obj.get("Unit_Tension6").toString())));

            List<Map<String,String>> mapList1=new ArrayList<>();
            HmiBaseRequest hmiBaseRequest1=new HmiBaseRequest();
            map.put("res_EN_WIDTH",caseManage.getExWidth());
            map.put("Deform_Rate1",caseManage.getDeformRate1());
            map.put("Deform_Rate2",caseManage.getDeformRate2());
            map.put("Deform_Rate3",caseManage.getDeformRate3());
            map.put("Deform_Rate4",caseManage.getDeformRate4());
            map.put("Deform_Rate5",caseManage.getDeformRate5());
            map.put("Roll_Num1",caseManage.getNr1());
            map.put("Roll_Num2",caseManage.getNr2());
            map.put("Roll_Num3",caseManage.getNr3());
            map.put("Roll_Num4",caseManage.getNr4());
            map.put("Roll_Num5",caseManage.getNr5());
            map.put("Unit_Tension1",caseManage.getUnitTension1());
            map.put("Unit_Tension2",caseManage.getUnitTension2());
            map.put("Unit_Tension3",caseManage.getUnitTension3());
            map.put("Unit_Tension4",caseManage.getUnitTension4());
            map.put("Unit_Tension5",caseManage.getUnitTension5());
            map.put("Unit_Tension6",caseManage.getUnitTension6());
            map.put("Diameter1",caseManage.getWrDia1());
            map.put("Diameter2",caseManage.getWrDia2());
            map.put("Diameter3",caseManage.getWrDia3());
            map.put("Diameter4",caseManage.getWrDia4());
            map.put("Diameter5",caseManage.getWrDia5());
            map.put("L",caseManage.getL());
            map.put("M",caseManage.getM());
            map.put("N",caseManage.getN());
            map.put("myu10",0.026);
            map.put("myu11",3.5);
            map.put("myu12",100);
            map.put("myu13",0);
            map.put("myu14",1.1);
            map.put("myu15",0.002);
            map.put("myu20",0.002);
            map.put("myu21",4.8);
            map.put("myu22",80);
            map.put("myu23",0);
            map.put("myu24",1.2);
            map.put("myu25",0.004);
            map.put("myu30",0.002);
            map.put("myu31",4.8);
            map.put("myu32",80);
            map.put("myu33",0);
            map.put("myu34",1.2);
            map.put("myu35",0.004);
            map.put("myu40",0.002);
            map.put("myu41",4.8);
            map.put("myu42",80);
            map.put("myu43",0);
            map.put("myu44",1.1);
            map.put("myu45",0.002);
            map.put("myu50",0.15);
            map.put("myu51",5);
            map.put("myu52",150);
            map.put("myu53",0);
            map.put("myu54",1);
            map.put("myu55",0);
            mapList1.add(map);
            Body body1=new Body();
            body1.setMsgId(2028);
            List<List<Map<String,String>>> list2=new ArrayList<>();
            list2.add(mapList);
            body1.setParams(list2);
            hmiBaseRequest1.setBody(body1);
            String json1 = JSON.toJSONString(hmiBaseRequest1);
            String respStr1 = httpClientService.postJsonRequest(hmi, json1, false, "");
            HmiBaseResponse responseDto1= JSONObject.parseObject(respStr1,HmiBaseResponse.class);
            if(responseDto1.getCode()==200){
                if(responseDto1.getResults().size()==0){
                    return;
                }
                Object object1=responseDto1.getResults().get(0);
                JSONArray picArray1=JSONArray.parseArray(object1.toString());
                JSONObject obj1= picArray1.getJSONObject(0);

                caseManage.setThk1(BigDecimal.valueOf(Double.parseDouble(obj1.get("EXITTHK1").toString())));
                caseManage.setThk2(BigDecimal.valueOf(Double.parseDouble(obj1.get("EXITTHK2").toString())));
                caseManage.setThk3(BigDecimal.valueOf(Double.parseDouble(obj1.get("EXITTHK3").toString())));
                caseManage.setThk4(BigDecimal.valueOf(Double.parseDouble(obj1.get("EXITTHK4").toString())));
                caseManage.setThk5(BigDecimal.valueOf(Double.parseDouble(obj1.get("EXITTHK5").toString())));

                caseManage.setSpeed1(BigDecimal.valueOf(Double.parseDouble(obj1.get("SPEED1").toString())));
                caseManage.setSpeed2(BigDecimal.valueOf(Double.parseDouble(obj1.get("SPEED2").toString())));
                caseManage.setSpeed3(BigDecimal.valueOf(Double.parseDouble(obj1.get("SPEED3").toString())));
                caseManage.setSpeed4(BigDecimal.valueOf(Double.parseDouble(obj1.get("SPEED4").toString())));
                caseManage.setSpeed5(BigDecimal.valueOf(Double.parseDouble(obj1.get("SPEED5").toString())));

                caseManage.setTension1(BigDecimal.valueOf(Double.parseDouble(obj1.get("TENSION1").toString())));
                caseManage.setTension2(BigDecimal.valueOf(Double.parseDouble(obj1.get("TENSION2").toString())));
                caseManage.setTension3(BigDecimal.valueOf(Double.parseDouble(obj1.get("TENSION3").toString())));
                caseManage.setTension4(BigDecimal.valueOf(Double.parseDouble(obj1.get("TENSION4").toString())));
                caseManage.setTension5(BigDecimal.valueOf(Double.parseDouble(obj1.get("TENSION5").toString())));

                caseManage.setRollForce1(BigDecimal.valueOf(Double.parseDouble(obj1.get("ROLLFORCE1").toString())));
                caseManage.setRollForce2(BigDecimal.valueOf(Double.parseDouble(obj1.get("ROLLFORCE2").toString())));
                caseManage.setRollForce3(BigDecimal.valueOf(Double.parseDouble(obj1.get("ROLLFORCE3").toString())));
                caseManage.setRollForce4(BigDecimal.valueOf(Double.parseDouble(obj1.get("ROLLFORCE4").toString())));
                caseManage.setRollForce5(BigDecimal.valueOf(Double.parseDouble(obj1.get("ROLLFORCE5").toString())));

                caseManage.setGap1(BigDecimal.valueOf(Double.parseDouble(obj1.get("GAP1").toString())));
                caseManage.setGap2(BigDecimal.valueOf(Double.parseDouble(obj1.get("GAP2").toString())));
                caseManage.setGap3(BigDecimal.valueOf(Double.parseDouble(obj1.get("GAP3").toString())));
                caseManage.setGap4(BigDecimal.valueOf(Double.parseDouble(obj1.get("GAP4").toString())));
                caseManage.setGap5(BigDecimal.valueOf(Double.parseDouble(obj1.get("GAP5").toString())));

                caseManage.setForslip1(BigDecimal.valueOf(Double.parseDouble(obj1.get("FORSLIP1").toString())));
                caseManage.setForslip2(BigDecimal.valueOf(Double.parseDouble(obj1.get("FORSLIP2").toString())));
                caseManage.setForslip3(BigDecimal.valueOf(Double.parseDouble(obj1.get("FORSLIP3").toString())));
                caseManage.setForslip4(BigDecimal.valueOf(Double.parseDouble(obj1.get("FORSLIP4").toString())));
                caseManage.setForslip5(BigDecimal.valueOf(Double.parseDouble(obj1.get("FORSLIP5").toString())));

                caseManage.setPower1(BigDecimal.valueOf(Double.parseDouble(obj1.get("MOTORPOWER1").toString())));
                caseManage.setPower2(BigDecimal.valueOf(Double.parseDouble(obj1.get("MOTORPOWER2").toString())));
                caseManage.setPower3(BigDecimal.valueOf(Double.parseDouble(obj1.get("MOTORPOWER3").toString())));
                caseManage.setPower4(BigDecimal.valueOf(Double.parseDouble(obj1.get("MOTORPOWER4").toString())));
                caseManage.setPower5(BigDecimal.valueOf(Double.parseDouble(obj1.get("MOTORPOWER5").toString())));
                caseManage.setStatus(1);

                optimizeDao.updateCase(caseManage);

            }else {
                throw new RuntimeException("计算仿真结果为空！");
            }
        }else {
            throw new RuntimeException("计算轧制规程参数为空！");
        }
    }
}
